package org.jash.common

import android.widget.ImageView
import androidx.databinding.BindingAdapter
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.ViewModelStoreOwner
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CircleCrop
import com.bumptech.glide.load.resource.bitmap.RoundedCorners

@BindingAdapter(value =["android:image_url", "android:roundedCornerRadius", "android:circleCrop"], requireAll = false)
fun loadImage(image: ImageView, url:String?, radius:Int = 0, circle: Boolean = false) {
    Glide.with(image)
        .load(url)
        .apply {
            if (radius > 0) {
                transform(RoundedCorners(radius))
            }
        }
        .apply {
            if (circle)  {
                transform(CircleCrop())
            }
        }
//        .placeholder(R.drawable.ic_launcher_foreground)
        .error(R.drawable.ic_launcher_background)
        .into(image)
}
inline fun <reified VM:ViewModel> ViewModelStoreOwner.viewModels()  =
    ViewModelProvider(this)[VM::class.java]
